package jdbc;

import jdbc.util.InputUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class UpdateUserDemo1 {
    public static void main(String[] args) {
        User user = InputUtil.getInputObject(new User(),"欢迎修改");
        System.out.println(user);

        try(Connection connection = DBUtil.getConnection();){
            String sql = "UPDATE user SET  password = ? " +
                         "WHERE username = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,user.getPassword());
            ps.setString(2,user.getUsername());

            int num = ps.executeUpdate();
            System.out.println(num > 0 ? "修改成功" : "修改失败");
            System.out.println("影响" + num + "行数据");
        }catch (SQLException e){
            e.printStackTrace();
        }

    }
}
